<template>
  <div class="customers-audit-container">
    <!-- 搜索选项 -->
    <div class="customers-audit-top">
      <el-row style="height:32px;line-height:32px">
        <el-col :span="6">
          <div class="customers-audit-top-col">
            <span style="padding:0 5px">审核状态</span>
            <el-select v-model="auditParams.type" placeholder="请选择" size="small">
              <el-option label="审核中" value="0"></el-option>
              <el-option label="审核成功" value="1"></el-option>
              <el-option label="审核失败" value="2"></el-option>
              <el-option label="未审核" value="3"></el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="10">
          <div class="customers-audit-top-col" style="padding-left:0">
            <span style="padding:0 5px">时间范围</span>
            <el-date-picker style="width:100%"
              v-model="time"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholde="开始时间"
              end-placeholde="结束时间"
              size="small">
            </el-date-picker>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="customers-audit-top-col">
            <span style="padding:0 5px">关键词</span>
            <el-input v-model="auditParams.searchName" placeholder="请输入" size="small"></el-input>
          </div>
        </el-col>
        
        
        <el-col :span="2">
          <el-button type="primary" size="small" @click="getAuditList">搜索</el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 表格 -->
    <div>
      <el-table :data="auditList" style="width: 100%" stripe>
        <el-table-column prop="id" label="会员ID" align="center"></el-table-column>
        <el-table-column label="头像-昵称" align="center">
          <template slot-scope="scope">
            <l-pic :picurl="scope.row.avatar" :size="{width:40,height:40}" :disdel="false" :disview="false"/>
            <div>{{ scope.row.nickname }}</div>
          </template>
        </el-table-column>
        <el-table-column label="姓名-手机号" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.realname }}</div>
            <div>{{ scope.row.phone }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="tuan_express_fee" label="团长配送费" align="center"></el-table-column>
        <el-table-column prop="user_balance" label="消费金额" align="center"></el-table-column>
        <el-table-column label="推荐人姓名" align="center"></el-table-column>
        <el-table-column label="审核人姓名" align="center"></el-table-column>
        <el-table-column label="城市" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.province + scope.row.city + scope.row.area }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="area_name" label="小区名称" align="center"></el-table-column>
        <el-table-column prop="format_create_time" label="申请时间" align="center"></el-table-column>
        <el-table-column label="审核状态" align="center">
          <template>
            <div>
              <el-button type="warning" plain @click="audit(scope.row.id)">待审核</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getTuanAuditList, doPusherAudit } from '@/api/customers'
import LPic from '@/components/lPic'
import { parseTime } from '@/utils/index'

export default {
  name: 'audit',
  components: {
    LPic
  },
  data() {
    return {
      page: 1,
      count: 1,

      auditList: [],

      auditParams: {
        key: this.$store.state.app.activeApp.saa_key,
        type: '3',
        limit: 10,
        page: this.page,
        is_self: 0,
        time: '',
        searchName: ''
      },

      time: '',

      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start,end]);
          }
        },
        {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start,end]);
          }
        }]
      }
    }
  },
  watch: {
    time(newTime) {
      if(newTime!==null){
        const startTime = parseTime(newTime[0],'{y}-{m}-{d}');
        const endTime = parseTime(newTime[1],'{y}-{m}-{d}');  
        this.auditParams.time = startTime + ' to ' + endTime;
      }else{
        this.auditParams.time = '';
      }        
    }
  },
  mounted() {
    this.getAuditList();
  },
  methods: {
    /**
     * 获取审核列表
     */
    getAuditList() {
      const params = this.auditParams;
      if(params.time === ''){
        delete params.time
      }
      if(params.searchName === ''){
        delete params.searchName
      }
      getTuanAuditList(params).then(res => {
        if(res.status === 200){
          this.auditList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204){
          this.auditList = [];
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 审核
     */
    audit(id) {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        id: id,
        status: 1,
      }
      this.$confirm('是否通过该审核?','提示',{
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        doPusherAudit(params).then(res => {
          if(res.status === 200) {
            this.$message.success('审核成功！');
            this.getAuditList();
          }else{
            this.$message.error(res.message);
          }
        })
      }).catch(() => {
        //取消
      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getAuditList();
    },
  }
}
</script>

<style lang="scss" scoped>
.customers-audit-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.customers-audit-top{
  margin: 20px auto;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 98%;
    overflow: hidden;
    display: flex;
    &>span{
      flex: 2;
      display: inline-block;
      padding: 0 5px;
      white-space: nowrap; 
      overflow: hidden;
    }
    &>div{
      flex: 4
    }
  }
}
</style>